<?php

namespace app\api\model;

use app\common\enums\StatusEnum;
use app\common\controller\Send;
use think\Db;

class Cache extends \app\common\model\Cache
{
    
    use send;
    /**
     * 增加缓存列表
     * @param $phone
     * @return mixed
     * @throws \think\Exception\DbException
     */
    public static function addCache($data)
    {
        if (empty($data['file_id']) || empty($data['type']) ) {
            return self::sendResult(141,[],'文件id或者缓存类型不能为空');
        }
        $cache = self::getCacheId($data);
        if ($cache) {
            return self::sendResult(141,[],'已经缓存过');
        }
        $type = $data['type'];
        //$type =1,2 视频音频,$type=3,4 单词和句子包缓存
        if($type==1 || $type ==2){
            //判断缓存的file_id
            $fileInfo = Db::table('lang_class_file')->field('a.id AS file_id,a.class_id,a.course_type,a.type AS file_type,b.lang_id,b.type,c.name,c.icon')
                        ->alias('a')
                        ->join('lang_class b', 'a.class_id = b.id')
                        ->join('lang c', 'b.lang_id = c.id')
                        ->where('a.id='.$data['file_id'])->select();
            
        }elseif($type==3 || $type ==4){
            //判断缓存的file_id
            $fileInfo = Db::table('lang_class_recite_type')->field('a.id AS file_id,a.class_id,a.course_type,a.name,b.lang_id,b.type,c.name,c.icon')
                        ->alias('a')
                        ->join('lang_class b', 'a.class_id = b.id')
                        ->join('lang c', 'b.lang_id = c.id')
                        ->where('a.id='.$data['file_id'])->select();
        }
        if(!$fileInfo){
            return self::sendResult(141,[],'没有该文件');
        }
        $fileInfo = $fileInfo[0];
        
        $time = time();
        $model = static::create([
            'oauth_id' => $data['uid'],
            'class_file_id' => $data['file_id'],
            'class_id' => $fileInfo['class_id'],
            'course_type' => $fileInfo['course_type'],
            'name' => $fileInfo['name'],
            'icon' => $fileInfo['icon'],
            'lang_id' => $fileInfo['lang_id'],
            'file_type' => $data['type'],
            'type' => $fileInfo['type'],
            'create_time' => $time,
            'update_time' => $time,
            'status' => 1
        ]);
        if($model->id){
            return self::sendResult(self::$okCode, [],'缓存成功');
        }else{
            return self::sendResult(141, [],'缓存失败');
        }
        
        
    }
    
    /**
     * 获取缓存列表
     * @param $phone
     * @return mixed
     * @throws \think\Exception\DbException
     */
    public static function getCacheList($data)
    {
    
        $cache_list = self::getCacheUid($data['uid']);
        if (empty($cache_list)) {
            return self::sendResult(141,[],'没有缓存信息');
        }
        foreach ($cache_list as $key=>$value){
            $cache_list[$key]['icon'] = config('file_prefix').$value['icon'];
        }
        return self::sendResult(self::$okCode, $cache_list,'获取成功');
    }
    
    
    /**
     * 获取单个缓存列表
     * @return mixed
     * @throws \think\Exception\DbException
     */
    public static function getOneCacheList($data)
    {
       
        $cache_list = self::getOneList($data);
        if(!$cache_list){
            return self::sendResult(141,[],'没有缓存信息');
        }
        $cache_list = $cache_list[0];
        $cache_list['icon'] = config('file_prefix').$cache_list['icon'];
        if($cache_list['video_num']>0){
            $cache_list['video_list'] = self::getLangClassFile($cache_list,1);
        }
        if($cache_list['audio_num']>0){
            $cache_list['audio_list'] = self::getLangClassFile($cache_list,2);
        }
        
        
        if($cache_list['word_num']>0){
            $cache_list['word_list'] = self::getReciteType($cache_list,1);
        }
        if($cache_list['sentence_num']>0){
            $cache_list['sentence_list'] = self::getReciteType($cache_list,2);
        }
        
        return self::sendResult(self::$okCode, $cache_list,'获取成功');
    }
    
    
    
    
    /**
     * 获取缓存列表
     * @param $cache_id
     * @return mixed
     * @throws \think\Exception\DbException
     */
    public static function deleteCacheList($reqData)
    {
    
        $where =array(
            'oauth_id' => $reqData['uid'],
            'class_id' => $reqData['class_id'],
            'lang_id' => $reqData['lang_id'],
            'type' => $reqData['type']
        );
    
        $cacheInfo = static::where($where)->select();
        if (!$cacheInfo) {
            return self::sendResult(118);
        }
        $ret = static::where($where)->delete();
        if($ret){
            return self::sendResult(self::$okCode, [], '删除成功');
        }else{
            return self::sendResult(119, [], '删除失败');
        }
    
    }
    
    /**
     * 获取缓存列表
     * @param $cache_id
     * @return mixed
     * @throws \think\Exception\DbException
     */
    public static function deleteOneCacheList($cacheId,$oauthId)
    {
    
        if ($cacheId <= 0) {
            return self::sendResult(102);
        }
        $where =array(
            'cache_id' => $cacheId
        );
        $cache = static::where($where)->find();
        if (empty($cache)) {
            return self::sendResult(118, [], '没有该缓存');
        }

        if ($cache['oauth_id'] != $oauthId) {
            return self::sendResult(119, [], '该缓存的用户不一致');
        }

        $ret = static::where([
            'cache_id' => $cacheId
        ])->delete();
        if($ret){
            return self::sendResult(self::$okCode, [], '删除成功');
        }else{
            return self::sendResult(119, [], '删除失败');
        }
        
    }
    
    
    /**
     * 删除多个缓存
     * @param $cache_id
     * @return mixed
     * @throws \think\Exception\DbException
     */
    public static function deleteMoreCacheList($cacheIds,$oauthId)
    {
    
        $cacheInfo = static::where('cache_id', 'in', $cacheIds)->where(['oauth_id' => $oauthId])->select();
        if (!$cacheInfo) {
            return self::sendResult(118, [], '没有该缓存');
        }
        $ret = static::where('cache_id', 'in', $cacheIds)->delete();
        if($ret){
            return self::sendResult(self::$okCode, [], '删除成功');
        }else{
            return self::sendResult(119, [], '删除失败');
        }
    
    }
     
}